﻿using System;
using System.IO.IsolatedStorage;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows;

namespace AdRotator
{
    public partial class DefaultHouseAd : UserControl
    {
        public object RemoteDefaultHouseAdBody;

        public Uri DefaultHouseAdURI;

        public delegate void OnAdFailed(object sender, EventArgs e);
        public delegate void OnAdLoaded(object sender, EventArgs e);
        public delegate void OnAdClicked(object sender, EventArgs e);

        public event OnAdFailed AdLoadingFailed;
        public event OnAdLoaded AdLoaded;
        public event OnAdClicked AdClicked;

        public DefaultHouseAd()
        {
            InitializeComponent();
        }

        public DefaultHouseAd(string URL = "")
        {
            if(!string.IsNullOrEmpty(URL)) DefaultHouseAdURI = new Uri(URL, UriKind.RelativeOrAbsolute);
            GetRemoteHouseAdControl();
            this.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(DefaultHouseAd_Tap);

            DataContext = this;

            InitializeComponent();
        }

        void DefaultHouseAd_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (AdClicked != null)
            {
                AdClicked("", new EventArgs());
            }
        }

        private void GetRemoteHouseAdControl()
        {
            try
            {
                //Remote Ad Process
                if (DefaultHouseAdURI != null)
                {
                    Networking.Network.GetStringFromURL(DefaultHouseAdURI, (s, e) =>
                    {
                        if (e != null)
                        {
                            if (IsolatedStorageSettings.ApplicationSettings.Contains("RemoteDefaultHouseAd"))
                            {
                                this.Content = (FrameworkElement)XamlReader.Load((string)IsolatedStorageSettings.ApplicationSettings["RemoteDefaultHouseAd"]);
                                if (AdLoaded != null)
                                {
                                    AdLoaded(null, new EventArgs());
                                }
                            }
                            else
                            {
                                if (AdLoadingFailed != null)
                                {
                                    AdLoadingFailed("Get Remote Ad Failed", new EventArgs());
                                }
                            }
                        }
                        else
                        {
                            this.Content = (FrameworkElement)XamlReader.Load(s);
                            if (IsolatedStorageSettings.ApplicationSettings.Contains("RemoteDefaultHouseAd"))
                            {
                                IsolatedStorageSettings.ApplicationSettings.Remove("RemoteDefaultHouseAd");
                            }
                            IsolatedStorageSettings.ApplicationSettings.Add("RemoteDefaultHouseAd", s);
                            if (AdLoaded != null)
                            {
                                AdLoaded(null, new EventArgs());
                            }
                        }
                    });
                }
            }
            catch
            {
                if (RemoteDefaultHouseAdBody == null)
                {
                    if (AdLoadingFailed != null)
                    {
                        AdLoadingFailed("", new EventArgs());
                    }
                }
            }
        }
    }
}
